package com.akeil.flash.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.wb.swt.SWTResourceManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.experimental.chart.swt.ChartComposite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.akeil.flash.action.DeleteDataAction;
import com.akeil.flash.action.OutputTestInfoLimsAction;
import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.contentprovider.DataContentProvider;
import com.akeil.flash.dialog.ChangeContentDialog;
import com.akeil.flash.event.DeviceInfoNotify;
import com.akeil.flash.event.ENotifyType;
import com.akeil.flash.event.FlashPointNotify;
import com.akeil.flash.labelprovider.DataLabelProvider;
import com.akeil.flash.layout.AutoResizeTableLayout;
import com.akeil.flash.model.EDeviceStatusType;
import com.akeil.flash.pojo.Data;
import com.akeil.flash.pojo.Parameter;
import com.akeil.flash.service.IDataService;
import com.akeil.flash.service.IParameterService;
import com.akeil.flash.utils.DataStatisticsUtil;

import flash.Activator;
import flash.Application;

@Component
public class ResultView extends ViewPart implements Observer {
	public static final String ID = "com.akeil.flash.view.ResultView";
	public static final String[] DATA_COLUMN_NAME = { "序号", "样品名称", "样品编号", "预置温度（℃）", "闪点（℃）", "测试时间" };
	TableColumn tableColumn = null;
	private DecimalFormat df2 = new DecimalFormat("0.00");
	private TableViewer viewerData;
	private Table tableData;
	Second current = new Second(); 
	private JFreeChart chart_main_temp = null;
	private ChartComposite frame_main_temp = null;
	private XYDataset dataset_temp;
	private TimeSeries series_temp;
	private double temperature;
	private double flashpoint;
	private double delta;
	private double atm;
	private Timer timer;
	List<Data> datas = new ArrayList<Data>();
	private Timer countTimer = null;
	private TimerTask countTimerTask;
	private boolean testRunning = false;
	private Scale scale = null;
	private double zero_voltage_s, min_voltage_s, max_voltage_s;
	@Autowired
	IParameterService parameterService;
	@Autowired
	IDataService dataService;
	private int count;
	private Text textCount;
	private Text textFlashPoint;
	private Composite compositeRightTop2;
	private Text textSampleTemperature;
	private Text textPreSettingTemperature;
	private Text textAfterTemperature;
	private Text textSampleName;
	private Text textSampleNumber;
	private Combo combo;
	List<Parameter> parameters;
	
	public ResultView() {
		timer = new Timer();
	}
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FormLayout());
		// 创建左上
		Composite compositeLeftTop = new Composite(container, SWT.NONE);
		FormData fd_compositeLeftTop = new FormData();
		fd_compositeLeftTop.top = new FormAttachment(0);
		fd_compositeLeftTop.left = new FormAttachment(0);
		fd_compositeLeftTop.bottom = new FormAttachment(100);
		fd_compositeLeftTop.right = new FormAttachment(60);
		compositeLeftTop.setLayoutData(fd_compositeLeftTop);

		// 创建右上1
		Composite compositeRightTop1 = new Composite(container, SWT.NONE);
		FormData fd_compositeRightTop1 = new FormData();
		fd_compositeRightTop1.top = new FormAttachment(0);
		fd_compositeRightTop1.left = new FormAttachment(60);
		fd_compositeRightTop1.bottom = new FormAttachment(65);
		fd_compositeRightTop1.right = new FormAttachment(100);
		compositeRightTop1.setLayoutData(fd_compositeRightTop1);
		compositeRightTop1.setLayout(new FormLayout());
		
				// 创建右上1_1
				Composite compositeRightTop1_1 = new Composite(compositeRightTop1, SWT.NONE);
				FormData fd_compositeRightTop1_1 = new FormData();
				fd_compositeRightTop1_1.top = new FormAttachment(0);
				fd_compositeRightTop1_1.left = new FormAttachment(0);
				fd_compositeRightTop1_1.bottom = new FormAttachment(100);
				fd_compositeRightTop1_1.right = new FormAttachment(90);
				compositeRightTop1_1.setLayoutData(fd_compositeRightTop1_1);
				
				// 创建右上1_2
				Composite compositeRightTop1_2 = new Composite(compositeRightTop1, SWT.NONE);
				FormData fd_compositeRightTop1_2 = new FormData();
				fd_compositeRightTop1_2.top = new FormAttachment(0);
				fd_compositeRightTop1_2.left = new FormAttachment(90);
				fd_compositeRightTop1_2.bottom = new FormAttachment(100);
				fd_compositeRightTop1_2.right = new FormAttachment(100);
				compositeRightTop1_2.setLayoutData(fd_compositeRightTop1_2);
		
		// 创建右上2
		compositeRightTop2 = new Composite(container, SWT.NONE);
		compositeRightTop2.setLayout(new GridLayout(4, false));
		FormData fd_compositeRightTop2 = new FormData();
		fd_compositeRightTop2.top = new FormAttachment(65);
		fd_compositeRightTop2.left = new FormAttachment(60);
		fd_compositeRightTop2.bottom = new FormAttachment(100);
		fd_compositeRightTop2.right = new FormAttachment(100);
		compositeRightTop2.setLayoutData(fd_compositeRightTop2);
		
		/*createLeftTopContent(compositeLeftTop);
		createRightTopContent1_1(compositeRightTop1_1);
		createRightTopContent1_2(compositeRightTop1_2);
		createRightTopContent2(compositeRightTop2);*/
		
		createLeftTopContent(compositeLeftTop);
		createRightTopContent1_1(compositeRightTop1_1);
		createRightTopContent1_2(compositeRightTop1_2);
		createRightTopContent2(compositeRightTop2);
		
		Label label = new Label(compositeRightTop2, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("滞后温度");
		
		textAfterTemperature = new Text(compositeRightTop2, SWT.BORDER);
		textAfterTemperature.setEnabled(false);
		textAfterTemperature.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textAfterTemperature.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textAfterTemperature.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(compositeRightTop2, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("样品名称");
		
		textSampleName = new Text(compositeRightTop2, SWT.BORDER);
		textSampleName.setEnabled(false);
		textSampleName.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textSampleName.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textSampleName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(compositeRightTop2, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("样品编号");
		
		textSampleNumber = new Text(compositeRightTop2, SWT.BORDER);
		textSampleNumber.setEnabled(false);
		textSampleNumber.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textSampleNumber.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textSampleNumber.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		setBaseSetting();
		
		// 其他设置
		Application.flashData.addObserver(this);
		startTimer();
	}

	private void createLeftTopContent(Composite parent) {
		parent.setLayout(new GridLayout(3, false));
		viewerData = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		tableData = viewerData.getTable();
		tableData.setLinesVisible(true);
		tableData.setHeaderVisible(true);
		tableData.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

		// / Popup Menu
				Menu contextMenu = new Menu(tableData);
				tableData.setMenu(contextMenu);
				
				MenuItem modifyItem = new MenuItem(contextMenu, SWT.None);
				modifyItem.setEnabled(false);
				modifyItem.setText("修改数据");
				modifyItem.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/view/modify_16px.png").createImage());
				modifyItem.addSelectionListener(new SelectionListener(){

					@Override
					public void widgetSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根
						TableItem[] selections = tableData.getSelection();
						Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
						ChangeContentDialog changeContentDialog = new ChangeContentDialog(parentShell);
						if(selections.length > 0){
							changeContentDialog.setTheData((Data)selections[0].getData());
							if(IDialogConstants.OK_ID == changeContentDialog.open()){
								dataService.updateByPrimayKey(changeContentDialog.getTheData());
								viewerData.refresh();
							}
						}
					}

					@Override
					public void widgetDefaultSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根
						
					}
				});
				
				MenuItem limsItem = new MenuItem(contextMenu, SWT.None);
				limsItem.setEnabled(false);
				limsItem.setText("输出LIMS文件");
				limsItem.addSelectionListener(new SelectionListener() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根
						TableItem[] selections = tableData.getSelection();
						if (selections.length > 0) {
							List<Data> selectedDatas = new ArrayList<Data>();
							for (TableItem tableItem : selections) {
								Data exdata = (Data) tableItem.getData();
								selectedDatas.add(exdata);
							}

							OutputTestInfoLimsAction outputTestInfoLimisAction = new OutputTestInfoLimsAction(selectedDatas);
							outputTestInfoLimisAction.run();
						} else {

						}
					}

					@Override
					public void widgetDefaultSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根

					}
				});
				
				MenuItem deleteItem = new MenuItem(contextMenu, SWT.None);
				deleteItem.setEnabled(false);
				deleteItem.setText("删除数据");
				deleteItem.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/view/delete_16px.png").createImage());
				deleteItem.addSelectionListener(new SelectionListener(){

					@Override
					public void widgetSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根
						TableItem[] selections = tableData.getSelection();
						if(selections.length > 0){
							List<Data> selectedDatas = new ArrayList<Data>();
							for (TableItem tableItem : selections) {
								Data data = (Data)tableItem.getData();
								selectedDatas.add(data);
							}
							
							DeleteDataAction deleteDataAction = new DeleteDataAction();
							deleteDataAction.setDatas(selectedDatas);
							deleteDataAction.run();
							viewerData.setInput(DataStatisticsUtil.processIndex(dataService.selectDataToday()));
						}else{
							
						}
					}

					@Override
					public void widgetDefaultSelected(SelectionEvent e) {
						// TODO 自动生成的方法存根
						
					}
				});
				
				
				tableData.addListener(SWT.MouseDown, new Listener() {

					@Override
					public void handleEvent(Event event) {
						TableItem[] selections = tableData.getSelection();
						if(selections.length > 0){
							modifyItem.setEnabled(true);
							limsItem.setEnabled(true);
							deleteItem.setEnabled(true);
						}else{
							modifyItem.setEnabled(false);
							limsItem.setEnabled(false);
							deleteItem.setEnabled(false);
						}
					}
				});
				
				tableData.addMouseListener(new MouseAdapter() {
					   @Override
					   public void mouseDoubleClick(MouseEvent e) {
						   TableItem[] selections = tableData.getSelection();
						   if(selections.length > 0){
								Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
								ChangeContentDialog changeContentDialog = new ChangeContentDialog(parentShell);
								if(selections.length > 0){
									changeContentDialog.setTheData((Data)selections[0].getData());
									if(IDialogConstants.OK_ID == changeContentDialog.open()){
										dataService.updateByPrimayKey(changeContentDialog.getTheData());
										viewerData.refresh();
									}
								}
						   }
					   }
					});
				
		AutoResizeTableLayout layout_data_sequence = new AutoResizeTableLayout(viewerData.getTable());
		for (int i = 0; i < DATA_COLUMN_NAME.length; i++) {
			tableColumn = new TableColumn(viewerData.getTable(), SWT.LEFT);
			tableColumn.setText(DATA_COLUMN_NAME[i]);
			viewerData.getTable().getColumn(i).pack();
			if(i == 0 ){
				layout_data_sequence.addColumnData(new ColumnWeightData(20));
			}else if(i == 5){
				layout_data_sequence.addColumnData(new ColumnWeightData(100));
			}else{
				layout_data_sequence.addColumnData(new ColumnWeightData(50));
			}
		}
		
		viewerData.setContentProvider(new DataContentProvider());
		viewerData.setLabelProvider(new DataLabelProvider());
		//设置字体，行高
		tableData.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		tableData.addListener(SWT.MeasureItem, new Listener() {
			@Override
			public void handleEvent(Event event) {
				// TODO 自动生成的方法存根
				// 设置宽度 
				event.width = tableData.getGridLineWidth();
				// 设置高度为字体高度的1.5倍
				event.height = (int) Math.floor(event.gc.getFontMetrics().getHeight() * 1.5);
			}
		});
		
		// 设置数据

		//initData();
		// 1 查询所有的数据
		//datas = dataService.selectAll();
		// 2 查询今天的数据	
		datas = dataService.selectDataToday();
		
		//重新计算Index
		viewerData.setInput(DataStatisticsUtil.processIndex(datas));
	}
	
	private void createRightTopContent1_1(Composite parent) {
		parent.setLayout(new FillLayout());
		
		TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
		tabFolder.setBounds(10, 10, 228, 446);
		 // Events
        tabFolder.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                TabItem item = tabFolder.getSelection()[0];
            }
        });
		
		TabItem tbtmSPeak = new TabItem(tabFolder, SWT.NONE);
		tbtmSPeak.setText("温度");
		tbtmSPeak.setControl(createTemperatureChart(tabFolder));
	}
	
	private void createRightTopContent1_2(Composite parent) {
		parent.setLayout(new FillLayout());
		scale = new Scale(parent, SWT.VERTICAL);
		scale.addMouseWheelListener(new MouseWheelListener() {
			public void mouseScrolled(MouseEvent e) {
				 Scale src = (Scale)e.getSource();
				 if(e.count > 0){
					 src.setSelection( src.getSelection() - 1 );
				 }else{
					 src.setSelection( src.getSelection() + 1 );
				 }
			}
		});
		scale.setMaximum(200);
        scale.setIncrement(1);
        scale.setPageIncrement(5);
        scale.setSelection(0);
        XYPlot xyplot = (XYPlot)chart_main_temp.getXYPlot();
        ValueAxis valueaxis_s = xyplot.getRangeAxis(0);
		valueaxis_s.setRange(0,100);
        
		scale.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int value = scale.getSelection();// 0 ~ 200
				min_voltage_s = 0;
				max_voltage_s = 100 + value;
				
				XYPlot xyplot = (XYPlot)chart_main_temp.getXYPlot();
				ValueAxis valueaxis_s = xyplot.getRangeAxis(0);
				valueaxis_s.setRange(min_voltage_s,max_voltage_s);
				valueaxis_s.setAutoRange(false);
			}
		});
			
		
	}
	
	private void createRightTopContent2(Composite parent) {
		Label label = new Label(parent, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("试验计时");
		
		textCount = new Text(parent, SWT.BORDER);
		textCount.setEditable(false);
		textCount.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textCount.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textCount.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
				
		Label label_3 = new Label(compositeRightTop2, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("试验步骤");
		
		combo = new Combo(compositeRightTop2, SWT.NONE);
		combo.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		combo.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		combo.setEnabled(false);
		combo.setItems(new String[] {"A", "B"});
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_3.setVisible(true);
			combo.setVisible(true);
		}else{
			label_3.setVisible(false);
			combo.setVisible(false);
		}
		
		Label lblNewLabel = new Label(compositeRightTop2, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		lblNewLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("样品温度");
		
		textSampleTemperature = new Text(compositeRightTop2, SWT.BORDER);
		textSampleTemperature.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textSampleTemperature.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textSampleTemperature.setEditable(false);
		textSampleTemperature.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(compositeRightTop2, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("预置温度");
		
		textPreSettingTemperature = new Text(compositeRightTop2, SWT.BORDER);
		textPreSettingTemperature.setEnabled(false);
		textPreSettingTemperature.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textPreSettingTemperature.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textPreSettingTemperature.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(parent, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_1.setText("闭口闪点");
		}else{
			label_1.setText("开口闪点");
		}
				
		textFlashPoint = new Text(parent, SWT.BORDER);
		textFlashPoint.setEditable(false);
		textFlashPoint.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textFlashPoint.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textFlashPoint.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		initCountTimer();
	}
	
	private ChartComposite createTemperatureChart(Composite parent) {
		dataset_temp = createSPeakDataset();
		chart_main_temp = ChartFactory.createTimeSeriesChart("样品温度图", "采样时间(s)", "温度值", dataset_temp, false, false, false);
		frame_main_temp = new ChartComposite(parent, SWT.NONE, chart_main_temp, true);
		XYPlot xyplot = (XYPlot)chart_main_temp.getPlot();
		ValueAxis valueaxis = xyplot.getRangeAxis();
		valueaxis.setRange(0, 300);
		valueaxis.setAutoRange(false);
		/*valueaxis = xyplot.getDomainAxis();
		valueaxis.setRange(0, 2700);
		valueaxis.setAutoRange(false);*/
		
		XYLineAndShapeRenderer localLineAndShapeRenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
	    localLineAndShapeRenderer.setUseFillPaint(true);
	    localLineAndShapeRenderer.setSeriesPaint(0,Color.red);//设置线条颜色
	    localLineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(1F));//设置线条粗细
	    xyplot.setRenderer(localLineAndShapeRenderer);
		frame_main_temp.pack();
		
		parent.layout();
		return frame_main_temp;
	}
	
	private XYDataset createSPeakDataset() {
		series_temp = new TimeSeries("Temperature Data", Second.class);
		series_temp.setMaximumItemAge(600);
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(series_temp);
		return dataset;
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
	/* (非 Javadoc) 
	* <p>Title: update</p> 
	* <p>Description: </p> 
	* @param o
	* @param arg 
	* @see java.util.Observer#update(java.util.Observable, java.lang.Object) 
	*/
	
	@Override
	public void update(Observable o, Object notify) {
		// TODO 自动生成的方法存根
		// 电压数据
		if (notify instanceof DeviceInfoNotify) {
			processTemperature(notify);
		}
		
		if(notify instanceof FlashPointNotify){
			processResult(notify);
		}
	}
	
	private void startTimer(){
		timer.schedule(new TimerTask(){

			@Override
			public void run() {
				frame_main_temp.getDisplay().asyncExec(new Runnable(){
					@Override
					public void run() {
						// TODO 自动生成的方法存根
						current = (Second) current.next();
						
						if(frame_main_temp != null  && frame_main_temp.getDisplay() != null && !frame_main_temp.getDisplay().isDisposed()){
							series_temp.add(current, temperature);
						}
					}
					
				});
			}
			
		}, 1000, 1000);
	}
	
	private void stopTimer(){
		timer.cancel();
		timer.purge();
	}
	
	private void processTemperature(Object notify) {
		final DeviceInfoNotify deviceInfoNotify = (DeviceInfoNotify) notify;
		
		if(deviceInfoNotify.getNotifyType().equals(ENotifyType.TEMPERATURE_INFO)){
			temperature = deviceInfoNotify.getValue();
			
			textSampleTemperature.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textSampleTemperature.setText(df2.format(temperature));
				}
				
			});
		}
		
		if(deviceInfoNotify.getNotifyType().equals(ENotifyType.FLASHPOINT_INFO)){
			textFlashPoint.setText(df2.format(deviceInfoNotify.getValue()));
		}
	}
	
	private void processResult(Object notify) {
		final FlashPointNotify flashPointNotify = (FlashPointNotify) notify;
		String samplename = parameters.get(0).getSamplename();
		String samplennumber = parameters.get(0).getSamplenumber();
		double previousSettingTemperature = parameters.get(0).getPretemperature();
		if(flashPointNotify.getNotifyType().equals(ENotifyType.FLASHPOINT_INFO)){
			flashpoint = flashPointNotify.getValue();
			
			Data data = new Data();
			data.setPretemperature(0D);
			data.setSamplename(samplename);
			data.setSampleno(samplennumber);
			data.setStatus(-1);
			data.setPretemperature(previousSettingTemperature);
			data.setFlashtemperature(flashpoint);
			data.setCorflashtemperature(flashpoint + delta);
			data.setAtm(atm);
			dataService.insert(data);
			
			viewerData.getTable().getDisplay().asyncExec(new Runnable(){
				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textFlashPoint.setText(df2.format(flashpoint));
					viewerData.setInput(DataStatisticsUtil.processIndex(dataService.selectDataToday()));
					ScrollBar sb = viewerData.getTable().getVerticalBar();
					if(sb != null && sb.isVisible()){
						sb.setSelection(sb.getMaximum());
						viewerData.getTable().setSelection(sb.getMaximum());
					} 
				}
			});
			
			testRunning = false;
			Application.flashData.setStatus(EDeviceStatusType.FINISH);
		}else if(flashPointNotify.getNotifyType().equals(ENotifyType.DELTA)){
			delta = flashPointNotify.getValue();
		}else if(flashPointNotify.getNotifyType().equals(ENotifyType.ATM_INFO)){
			atm = flashPointNotify.getValue();
		}
	}
	public void stopTest(){
		testRunning = false;
	}
	
	public void readyTest(){
		count = 0;
		if(textFlashPoint != null && (!textFlashPoint.isDisposed())){
			textFlashPoint.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textFlashPoint.setText("");
				}
			});
		}
		testRunning = false;
	}
	
	public void startTest(){
		count = 0;
		testRunning = true;
		if(textFlashPoint != null && (!textFlashPoint.isDisposed())){
			textFlashPoint.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textFlashPoint.setText("");
				}
				
			});
		}
	}
	
	private void initCountTimer(){
		countTimer = new Timer();
		countTimerTask = new TimerTask(){

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				if(textCount != null && (!textCount.isDisposed())){
					textCount.getDisplay().asyncExec(new Runnable(){
						@Override
						public void run() {
							// TODO 自动生成的方法存根
							count++;
							
							if((textCount != null) && (!textCount.isDisposed()) && testRunning){
								//textCount.setText(String.valueOf(count++));
								textCount.setText(String.format("%02d:%02d",count / 60 , count % 60));
							}
						}
					});
					
				}
			}
			
		};
		countTimer.schedule(countTimerTask,1000,1000);
	}
	
	public void setBaseSetting(){
		parameters = parameterService.selectAll();
		
		textPreSettingTemperature.setText(df2.format(parameters.get(0).getPretemperature()));
		textAfterTemperature.setText(df2.format(parameters.get(0).getNexttemperature()));
		textSampleName.setText(parameters.get(0).getSamplename());
		textSampleNumber.setText(parameters.get(0).getSamplenumber());
		combo.select(parameters.get(0).getMethod().equals("A") ? 0 : 1);
	}
}